from user.models import User, Role
from project.utils.list import list_projects
from technology.utils.list import list_technologies_by_user
from user.utils.permission import get_pids_by_rid


def user_view(user):
    assert isinstance(user, User)
    view = {
        'id': user.id,
        'name': user.name,
        'role': {
            'id': user.rid_id,
            'name': Role.objects.get(id=user.rid_id).name
        },
        'projects': [{'id': proj.id, 'name': proj.name} for proj in list_projects(user)],
        'technologies': [{'id': tech.id, 'name': tech.name} for tech in list_technologies_by_user(user)]
    }
    return view


def role_view(role):
    assert isinstance(role, Role)
    view = {
        'id': role.id,
        'name': role.name,
        'permissions': list(get_pids_by_rid(role))
    }
    return view
